﻿package idv.cjcat.arrowwalker {
	import com.pblabs.engine.core.ProcessManager;
	import com.pblabs.engine.entity.allocateEntity;
	import com.pblabs.engine.entity.IEntity;
	import com.pblabs.engine.entity.PropertyReference;
	import com.pblabs.engine.PBE;
	import com.pblabs.rendering2D.DisplayObjectRenderer;
	import com.pblabs.rendering2D.DisplayObjectScene;
	import com.pblabs.rendering2D.SimpleSpatialComponent;
	import com.pblabs.rendering2D.ui.SceneView;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import idv.cjcat.arrowwalker.components.SimpleController;
	
	public class ArrowWalker extends MovieClip {
		
		//object mask constants
		public static const RENDERABLE:String = "renderable";
		
		//property reference constants
		public static const SPATIAL:String = "spatial";
		public static const POSITION_REF:PropertyReference = new PropertyReference("@" + SPATIAL + ".position");
		public static const ROTATION_REF:PropertyReference = new PropertyReference("@" + SPATIAL + ".rotation");
		
		private var scene:DisplayObjectScene;
		
		public function ArrowWalker() {
			PBE.startup(this);
			setupScene();
			setupArrow();
			
			//force the virtual time to advance in order to cancle the "initial jump" effect
			ProcessManager.instance.testAdvance(0);
		}
		
		private function setupScene():void{
			var sceneEntity:IEntity = allocateEntity();
			sceneEntity.initialize("sceneEntity");
			
			scene = new DisplayObjectScene();
			
			var sceneView:SceneView = new SceneView();
			sceneView.width = 640;
			sceneView.height = 480;
			scene.sceneView = sceneView;
			
			sceneEntity.addComponent(scene, "renderer");
		}
		
		private function setupArrow():void{
			var arrowEntity:IEntity = allocateEntity();
			arrowEntity.initialize("arrowEntity");
			
			var spatial:SimpleSpatialComponent = new SimpleSpatialComponent();
			var renderer:DisplayObjectRenderer = new DisplayObjectRenderer();
			var controller:SimpleController = new SimpleController();
			
			//setup renderer componenet (arrow_mc is a sprite in the FLA file)
			renderer.displayObject = arrow_mc;
			renderer.positionProperty = POSITION_REF;
			renderer.rotationProperty = ROTATION_REF;
			scene.add(renderer);
			
			arrowEntity.addComponent(spatial, SPATIAL);
			arrowEntity.addComponent(renderer, "renderer");
			arrowEntity.addComponent(controller, "controller");
		}
	}
}